package com.ruoyi.web.controller.cms.article;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cms.fileInfo.domain.CmsFileInfo;
import com.ruoyi.cms.fileInfo.service.ICmsFileInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cms.fileInfo.domain.CmsArticleFile;
import com.ruoyi.cms.fileInfo.service.ICmsArticleFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章文件管理Controller
 * TODO 预期作废
 *
 * @author linyang
 * @date 2025-07-22
 */
@RestController("AdminCmsArticleFileController")
@RequestMapping("/admin/cms/article/file")
public class CmsArticleFileController extends BaseController {


    @Resource
    private ICmsArticleFileService cmsArticleFileService;


    @Resource
    private ICmsFileInfoService cmsFileInfoService;


    /**
     * 新增article文件关联
     */
    //@PreAuthorize("@ss.hasPermi('cms:article:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CmsArticleFile cmsArticleFile) {
        List<CmsArticleFile> cmsArticleFileList = new ArrayList<>();
        Long articleId = cmsArticleFile.getArticleId();
        Long[] fileIds = cmsArticleFile.getFileIds();
        for (Long fileId : fileIds) {
            CmsArticleFile info = new CmsArticleFile();
            info.setFileId(fileId);
            info.setArticleId(articleId);
            cmsArticleFileList.add(info);
        }
        cmsArticleFileService.batchAddCmsArticleFile(cmsArticleFileList);
        return success();
    }

    /**
     * 删除article文件关联
     */
    //@PreAuthorize("@ss.hasPermi('cms:article:remove')")
    @DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds) {
        for (Long id : articleIds) {
            List<CmsArticleFile> cmsArticleFileList = cmsArticleFileService.selectCmsArticleFileListByArticleId(id);
            cmsArticleFileList.forEach((CmsArticleFile cmsArticleFile) -> {
                Long fileId = cmsArticleFile.getFileId();
                cmsFileInfoService.deleteCmsFileInfoByFileId(fileId);//删除
            });
        }
        cmsArticleFileService.deleteCmsArticleFileByArticleIds(articleIds);
        return toAjax(1);
    }

    /**
     * 根据articleId获取文件列表
     */
    //@PreAuthorize("@ss.hasPermi('cms:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable Long articleId) {
        List<CmsFileInfo> sysFileInfoList = new ArrayList<>();
        List<CmsArticleFile> fileBlogInfos = cmsArticleFileService.selectCmsArticleFileListByArticleId(articleId);
        fileBlogInfos.forEach((CmsArticleFile cmsArticleFile) -> {
            Long fileId = cmsArticleFile.getFileId();
            CmsFileInfo sysFileInfo = cmsFileInfoService.selectCmsFileInfoByFileId(fileId);
            if (sysFileInfo != null) {
                sysFileInfoList.add(sysFileInfo);
            }
        });
        return AjaxResult.success(sysFileInfoList);
    }

    /**
     * 查询文章文件管理列表
     */
//    @PreAuthorize("@ss.hasPermi('cms:file:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(CmsArticleFile cmsArticleFile) {
//        startPage();
//        List<CmsArticleFile> list = cmsArticleFileService.selectCmsArticleFileList(cmsArticleFile);
//        return getDataTable(list);
//    }

    /**
     * 导出文章文件管理列表
     */
//    @PreAuthorize("@ss.hasPermi('cms:file:export')")
//    @Log(title = "文章文件管理", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, CmsArticleFile cmsArticleFile) {
//        List<CmsArticleFile> list = cmsArticleFileService.selectCmsArticleFileList(cmsArticleFile);
//        ExcelUtil<CmsArticleFile> util = new ExcelUtil<CmsArticleFile>(CmsArticleFile.class);
//        util.exportExcel(response, list, "文章文件管理数据");
//    }

    /**
     * 获取文章文件管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('cms:file:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id) {
//        return success(cmsArticleFileService.selectCmsArticleFileById(id));
//    }

    /**
     * 新增文章文件管理
     */
//    @PreAuthorize("@ss.hasPermi('cms:file:add')")
//    @Log(title = "文章文件管理", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody CmsArticleFile cmsArticleFile) {
//        return toAjax(cmsArticleFileService.insertCmsArticleFile(cmsArticleFile));
//    }

    /**
     * 修改文章文件管理
     */
//    @PreAuthorize("@ss.hasPermi('cms:file:edit')")
//    @Log(title = "文章文件管理", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody CmsArticleFile cmsArticleFile) {
//        return toAjax(cmsArticleFileService.updateCmsArticleFile(cmsArticleFile));
//    }

    /**
     * 删除文章文件管理
     */
//    @PreAuthorize("@ss.hasPermi('cms:file:remove')")
//    @Log(title = "文章文件管理", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(cmsArticleFileService.deleteCmsArticleFileByIds(ids));
//    }
}
